package pong.game.user;

import javax.microedition.khronos.opengles.GL10;

import pong.game.objects.GL_ActiveItem;
import pong.game.objects.GL_Handle;
import pong.mathematic.Vector2D;
import pong.mathematic.Vector3D;
import android.util.Log;

public class Player extends GL_Handle {

	private static final String TAG = "Player";

	private static final int IMPULSE_MIN_PERIOD = 5000;
	private static final int IMPULSE_ANIMATION_TIME = 250;

	private static final float IMPULSE_FROM_SCALE = 0;
	private static final float IMPULSE_TO_SCALE = 6;
	private static final float IMPULSE_ROTATE_SPEED = 360;
	private static final float IMPULSE_FROM_ALPHA = 1;
	private static final float IMPULSE_TO_ALPHA = 0;

	private String userName = null;
	private int playerId = 0;
	private GL_ActiveItem impulse = null;
	private long prevImpulseStart = 0;

	public Player(String userName, Vector3D center, float width, float height) {
		super(center, width, height);
		impulse = new GL_ActiveItem(center, width, height) {
			@Override
			public boolean isIn(Vector3D clickPos) {
				return isIn(clickPos, 0);
			}

			@Override
			public boolean isIn(Vector3D clickPos, float addDistance) {
				return center.distance(clickPos) <= radius + addDistance;
			}

			@Override
			public boolean isIn(Vector2D clickPos) {
				return isIn(clickPos, 0);
			}

			@Override
			public boolean isIn(Vector2D clickPos, float addDistance) {
				return center.distance(clickPos) <= radius + addDistance;
			}
		};

		impulse.setAlpha(0);
		impulse.setScaleValue(IMPULSE_TO_SCALE);
		this.userName = userName;
		playerId = Party.idCounter++;
	}

	public GL_ActiveItem getImpulse() {
		return impulse;
	}

	@Override
	public void render(GL10 gl, float[] mModelMatrix, float[] mMVPMatrix, float[] mViewMatrix,
			float[] mProjectionMatrix, int mProgramHandle, float[] mLightPosInEyeSpace) {
		impulse.render(gl, mModelMatrix, mMVPMatrix, mViewMatrix, mProjectionMatrix, mProgramHandle,
				mLightPosInEyeSpace);
		super.render(gl, mModelMatrix, mMVPMatrix, mViewMatrix, mProjectionMatrix, mProgramHandle, mLightPosInEyeSpace);
	}

	public void impulse() {
		if (System.currentTimeMillis() - prevImpulseStart > IMPULSE_MIN_PERIOD) {
			Log.i(TAG, "impulse !!!");
			prevImpulseStart = System.currentTimeMillis();
			impulse.startAlphaAnimation(IMPULSE_FROM_ALPHA, IMPULSE_TO_ALPHA, IMPULSE_ANIMATION_TIME);
			impulse.startScaleAnimation(IMPULSE_FROM_SCALE, IMPULSE_TO_SCALE, IMPULSE_ANIMATION_TIME);
			impulse.startRotateAnimation(impulse.getRotationAngle(), impulse.getRotationAngle() + IMPULSE_ROTATE_SPEED,
					IMPULSE_ANIMATION_TIME);
		}
	}
}
